﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
using UITimer = System.Windows.Forms.Timer;

namespace WinFormsApp4
{
    public partial class Form2 : Form
    {
        // 初始化变量
        int timeLimit; // 时间限制
        int correctAnswer; // 正确答案
        bool isOutOfTime; // 是否超时
        double totalQuestions; // 总问题数量
        double correctQuestions; // 正确答案数量
        double currentNum; // 当前回答问题总数
        UITimer questionTimer; // 问题定时器
        private DateTime startTime;
        private UITimer totalTimer; // 总计时器
        private Form resultForm; // 结果窗体
        Random random; // 随机数生成器
        int remainingSeconds; // 剩余秒数

        public Form2(int timeLimit, int totalQuestions)
        {
            random = new Random();
            this.timeLimit = timeLimit;
            this.totalQuestions = totalQuestions;

            InitializeComponent();

            // 设置默认按钮为按钮1
            this.AcceptButton = button1;

            currentNum = 0;
            correctQuestions = 0;
            isOutOfTime = false;
            SetQuestion();

            // 初始化总计时器
            totalTimer = new UITimer();
            totalTimer.Interval = 1000; // 设置总计时器的间隔为1秒
            totalTimer.Tick += TotalTimer_Tick;
            startTime = DateTime.Now;
            totalTimer.Start(); // 启动总计时器
        }

        // 更新总计时器显示
        private void UpdateTimerDisplay()
        {
            int hours = remainingSeconds / 3600;
            int minutes = (remainingSeconds % 3600) / 60;
            int seconds = remainingSeconds % 60;

            label5.Text = $"Remaining Time: {hours:D2}:{minutes:D2}:{seconds:D2}";
        }

        // 总计时器的Tick事件处理程序
        private void TotalTimer_Tick(object sender, EventArgs e)
        {
            TimeSpan elapsedTime = DateTime.Now - startTime;
            label4.Text = "Total Time: " + elapsedTime.ToString(@"hh\:mm\:ss");

            if (remainingSeconds > 0)
            {
                remainingSeconds--;
                UpdateTimerDisplay();
            }
            else if(string.IsNullOrEmpty(label3.Text.ToString()))
            {
                label3.Text = "Out of Time";
                isOutOfTime = true;
            }
        }

        // "下一题"按钮点击事件处理程序
        private void button1_Click(object sender, EventArgs e)
        {
            NextOperation();
        }

        // 下一操作
        private void NextOperation()
        {
            if (currentNum != totalQuestions && string.IsNullOrEmpty(label3.Text.ToString()))
            {
                if (int.TryParse(textBox1.Text, out int userAnswer))
                {
                    if (userAnswer == correctAnswer)
                    {
                        label3.Text = "Correct";
                        correctQuestions++;
                    }
                    else
                    {
                        label3.Text = "Wrong";
                    }
                }
                else
                {
                    // 用户输入无效的整数，你可以在这里处理错误情况，例如显示一个消息框
                    MessageBox.Show("Please enter a valid integer answer!");
                }
            }
            else
            {
                NextQuestion();
            }
        }

        // 下一题
        private void NextQuestion()
        {
            currentNum++;
            label6.Text = "Accuracy: " + correctQuestions.ToString() + "/" + currentNum.ToString();
            if (currentNum != totalQuestions)
            {
                SetQuestion();
            }
            else
            {
                resultForm = new Form3(label4.Text.ToString(), label6.Text.ToString());
                this.Hide();
                resultForm.ShowDialog();
                this.Close();
            }
        }

        // 设置问题
        private void SetQuestion()
        {
            remainingSeconds = timeLimit;
            isOutOfTime = false;
            int number1 = random.Next(100);
            int number2 = random.Next(100);
            string operation = random.Next(2) == 1 ? "+" : "-";
            label2.Text = $"{number1} {operation} {number2} =";
            correctAnswer = operation == "+" ? number1 + number2 : number1 - number2;
            label3.Text = "";
            textBox1.Text = ""; // 修正了此处的空格
        }

        // 窗体初始化
        public Form2()
        {
            InitializeComponent();
        }

        // 其他事件处理程序
        private void label3_Click(object sender, EventArgs e)
        {
        }

        private void label2_Click(object sender, EventArgs e)
        {
        }

        private void label4_Click(object sender, EventArgs e)
        {
        }

        private void label5_Click(object sender, EventArgs e)
        {
        }

        private void label6_Click(object sender, EventArgs e)
        {
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }
    }
}


